package com.lanyam.travel.base.controller;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyam.travel.base.annotation.NoRepeatSubmit;
import com.lanyam.travel.base.entity.User;
import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.utils.RedisUtil;

@RequestMapping("/redis")
@RestController
public class RedisController {
	private static int ExpireTime = 60;   // redis中存储的过期时间60s

    @Resource
    private RedisUtil redisUtil;
    
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("set")
    public boolean redisset(String key, String value){
        ValueOperations<String, String> operations = redisTemplate.opsForValue();

        User userEntity =new User();
        userEntity.setId("0001");
        userEntity.setName("张三");
        userEntity.setPhone("18896727633");
        userEntity.setPassword("123456");

        //return redisUtil.set(key,userEntity,ExpireTime);
        operations.set(key+"_1", value, 1,TimeUnit.MINUTES);
        return redisUtil.set(key,value);
    }

    @RequestMapping("get")
    public Object redisget(String key){
        return redisUtil.get(key);
    }

    @RequestMapping("expire")
    public boolean expire(String key){
        return redisUtil.expire(key,ExpireTime);
    }
    
    @RequestMapping("/test")
    @NoRepeatSubmit
    public JSONResult test() {
        return JSONResult.ok("程序逻辑返回");
    }

}
